document.write("<script type='application/javascript' src='../../js/dialog/main.js'></script>");
document.write("<script type='application/javascript' src='../../js/date/main.js'></script>");
document.write("<script type='application/javascript' src='../../js/vue/vue.js'></script>");
document.write("<script type='application/javascript' src='../../js/vue/vue-resource.min.js'></script>");
document.write("<script type='application/javascript' >"+
	"Vue.http.interceptors.push(function(request,next){" +
                    "var open_id = localStorage.getItem('open_id');"+
					"request.headers.set('openid', open_id);" +
                    "request.headers.set('openid', open_id);" +
                    "next(function(response){" +
                    "return response;" +
                    "});" +
                    "})"+ 
"</script>");

var local = true;

// var baseUrl = "https://"+window.location.hostname+"/";
var baseUrl = "https://app.meijiangzi.com/";

var imgUrl = baseUrl + "static/";

// 页面路径管理JSON
var page = {
	index: {
		index: "project/index.html", 
		detail: "project/detail.html",
		list: "project/list.html",
		city: "project/city.html",
		search: "pzym/search.html",
		confirm_booking: "project/confirm_booking.html"
	},
	pzym: {
		index: "pzym/index.html"
	},
	product: {
		index: "product/index.html",
		detail: "product/detail.html",
		type: "product/type.html",
		type_detail: "product/type_detail.html"
	},
	mine: {
		index: "mine/index.html",
		login:"mine/login.html",
		setting:"mine/setting.html",
		detail:"mine/detail.html",
		wallet: "mine/wallet.html",
		points:"mine/points.html",
		cards:"mine/cards.html",
		addCards: "mine/addCards.html",
		cashOut: "mine/cashOut.html",
		agreements: "mine/user_agreements.html",
		share:"mine/share.html",
		friend:"mine/friend.html",
		qrcode:"mine/qrcode.html", 
		sign: "mine/sign.html", 
		onlineService: "mine/onlineService.html",
		luck:"mine/luck.html"
	},
	shopcar:{
		index:"shopcar/index.html"
	},
	search:{
		searchAllPage: "search/searchAll.html", 
		input:"search/input.html"
	},
	eval:{
		index:"eval/index.html",
		evaluate:"eval/evaluate.html",
		result:"eval/result.html"
	},
	order:{
		preview:"order/preview.html",
		index:"order/index.html",
		detail:"order/detail.html",
		projectOrder: "order/projectOrder.html", 
		projectOrderDetail: "order/projectOrderDetail.html"
	},
	address:{
		index:"address/index.html",
		add:"address/add.html"
	},
	collect:{
		index:"collect/index.html"
	},
	logistics:{
		index:"logistics/index.html"
	},
	pay:{
		result: "pay/result.html", 
		type:"pay/type.html"
	}, 
	activity: {
		index: "activity/activity.html"
	}, 
	hospital: {
		baike: "hospital/baike.html",
		baike_detail: "hospital/baike_detail.html", 
		hospital: "hospital/hospital.html",
		hospital_detail: "hospital/hospital_detail.html",
		doctor: "hospital/doctor.html",
		doctor_detail: "hospital/doctor_detail.html"
	}
}

// 获取客户端高度 len 减去的高度
function setHeight(len) {
	if (!len) len = 0;
	var height = document.documentElement.clientHeight - len;
	document.getElementsByTagName('body')[0].style.height = height + 'px';
}

var getTolist = function(){
	var tolist = localStorage.getItem("tolist");
	if (!tolist) {
		tolist = new Array();
	} else {
		tolist = JSON.parse(tolist);
	}
	return tolist;
}

var cur = window.document.location.href;
var curPage = cur.substring(cur.indexOf("html/") + 5);

// 跳转
var Jump = {
	// 前往某个页面
	to: function(url, param, curParam) {
		if (!url) {
			console.error("请在static_common.js的page对象中配置文件地址!");
			return;
		}
		var tolist = getTolist(); 
		// 如果前往的是主页面 把页面栈置空
		if (url === page.index.index || url === page.pzym.index || url === page.product.index|| url === page.mine.index) {
			tolist = new Array();
		} else {
			// 当前页面放入页面栈中
			if (curParam) {
				if (curPage.indexOf("?") != -1) {
					curPage = curPage.substring(0, curPage.indexOf("?"));
				}
				curPage += "?" + curParam;
			}
			// 如果已经存在过了 就把之前的移除放入最新的
			tolist.forEach(function(e, index, arr) {
				var start = e.split("?");
				if (curPage.indexOf(start[0]) != -1) {
					arr.splice(index, 1);
				}
			});
			tolist.push(curPage);
		}
		if (param) {
			url += "?" + param;
		}
		localStorage.setItem("tolist", JSON.stringify(tolist));
		openWb(url);
	},
	// 返回上一个页面
	back: function() {
		var tolist = getTolist();
		var last = tolist.pop();
		if (!last) {
			return;
		}
		// 如果回退到主页面 将页面栈置空
		if (last === page.index.index || last === page.pzym.index || last === page.product.index || last === page.mine.index) {
			tolist = new Array();
		}
		localStorage.setItem("tolist", JSON.stringify(tolist));
		plus.webview.currentWebview().hide();
		openWb(last);
	}
}

var reloadPage = ['project/confirm_booking.html', 'mine/login.html', 'mine/wallet.html', 'mine/points.html', 'mine/cards.html', 'mine/cashOut.html', 'mine/friend.html', 'mine/luck.html', 'shopcar/index.html', 'order/'];

var openWb = function(url){
	//document.addEventListener('plusready', function(){
		var tempWebview = plus.webview.getWebviewById(url); 
		if(tempWebview){
			//需要刷新的页面 reload
			var isReloadPage = false; 
			reloadPage.forEach(function(i){
				if(url.indexOf(i) != -1){
					isReloadPage = true; 
					return false;
				}
			});
			if(isReloadPage){
				tempWebview.reload();
			}
			tempWebview.show();
		}else{
			plus.webview.open('../../html/'+url, url);
		}
	// }, false);
}

var footerHtml = "<ul class='_ul data-v-65ddb5fe bottom-nav' id='footer'>" +
	"<li class='_li data-v-65ddb5fe nav-item' data-comkey='0' data-eventid='0' onclick=\"Jump.to('" + page.index.index + "')\">" +
	"<div class='_div data-v-65ddb5fe nav-icon'><img class='_img data-v-65ddb5fe image' src='../../images/index/shouye--hui@2x.png' /></div>" +
	"<h4 class='_h4 data-v-65ddb5fe nav-text'>首页</h4></li>" +
	"<li class='_li data-v-65ddb5fe nav-item' data-comkey='0' data-eventid='1' onclick=\"Jump.to('" + page.pzym.index +
	"')\">" +
	"<div class='_div data-v-65ddb5fe nav-icon'><img class='_img data-v-65ddb5fe image' src='../../images/index/pinzhiyimei--hui@2x.png' /></div>" +
	"<h4 class='_h4 data-v-65ddb5fe nav-text'>品质医美</h4></li>" +
	"<li class='_li data-v-65ddb5fe nav-item'>"+
	"<div class='_div data-v-65ddb5fe nav-btn'>"+
	"<button bindtap='handleProxy' class='_button data-v-65ddb5fe btn' onclick=\"Jump.to('" + page.order.projectOrder + "');\">+</button>"+
	"</div>"+
	// "<div class='_div sign-btn'>"+
	// "<button bindtap='handleProxy' class='_button btn' onclick=\"Jump.to('" + page.mine.sign + "');\"></button>"+
	// "</div>"+
	"</li>"+
	"<li class='_li data-v-65ddb5fe nav-item' data-comkey='0' data-eventid='3' onclick=\"Jump.to('" + page.product.index +
	"')\">" +
	"<div class='_div data-v-65ddb5fe nav-icon'><img class='_img data-v-65ddb5fe image' src='../../images/index/shangcheng--hui@2x.png' /></div>" +
	"<h4 class='_h4 data-v-65ddb5fe nav-text'>积分商城</h4></li>" +
	"<li class='_li data-v-65ddb5fe nav-item' data-comkey='0' data-eventid='4' onclick=\"Jump.to('" + page.mine.index +
	"')\">" +
	"<div class='_div data-v-65ddb5fe nav-icon'><img class='_img data-v-65ddb5fe image' src='../../images/index/wode--hui@2x.png' /></div>" +
	"<h4 class='_h4 data-v-65ddb5fe nav-text'>我的</h4></li></ul>";

var headerHtml = "<div class='nvbt iback' onclick='Jump.back()'></div><div class='nvtt'></div>"

// 动态创建html
var createHtml = {
	// 动态创建页面底部
	footer: function() {
		var footers = document.getElementsByTagName("footer");
		if (!footers) {
			return;
		}
		if (footers.length == 0) {
			return;
		}
		var footer = footers[0];
		footer.innerHTML = footerHtml;
		var active = footer.getAttribute("active");
		if (!active) {
			return;
		}
		active = parseInt(active);
		if (isNaN(active)) {
			return;
		}
		var li = footer.getElementsByTagName("li")[active < 2 ? active : active+1];
		li.removeAttribute("onclick");
		var h4 = footer.getElementsByTagName("h4")[active];
		h4.classList.add("active");
		var img = footer.getElementsByTagName("img")[active];
		img.setAttribute("src", img.getAttribute("src").replace("hui", "hong"))
	},
	// 创建页面头部
	header: function() {
		var headers = document.getElementsByTagName("header");
		if (!headers) {
			return;
		}
		if (headers.length == 0) {
			return;
		}
		var header = headers[0];
		header.innerHTML = headerHtml;
		var title = header.getAttribute("title");
		if (!title) {
			return;
		}
		var headerTitle = header.getElementsByTagName("div")[1];
		headerTitle.innerText = title;
	},
	excute: function() {
		this.footer();
		this.header();
	}

}

// 获取URL中的参数
var URLParam = {
	result: null,
	init: function() {
		var index = cur.indexOf('?')
		var str = cur.substring(index + 1);
		var arr = str.split('&');
		this.result = {};
		var t = this;
		arr.forEach(function(item){
			var a = item.split('=');
			t.result[a[0]] = decodeURI(decodeURI(a[1]));
		});
	},
	get: function(e) {
		if (!this.result) {
			this.init();
		}
		return this.result[e];
	}
}

var httpIpPort=window.location.href.substring(0,window.location.href.indexOf("mjz")-1);

document.ready = function() {
	createHtml.excute();
}

var srollEvent = {
	getScrollTop: function() {
		// 考虑到浏览器版本兼容性问题，解析方式可能会不一样
		return document.documentElement.scrollTop || document.body.scrollTop;
	},
	getWinHeight: function() {
		return document.documentElement.clientHeight || document.body.clientHeight;
	},
	getScrollHeight: function() {
		bodyScrollHeight = 0
		documentScrollHeight = 0
		if (document.body) {
			bodyScrollHeight = document.body.scrollHeight
		}
		if (document.documentElement) {
			documentScrollHeight = document.documentElement.scrollHeight
		}
		// 当页面内容超出浏览器可视窗口大小时，Html的高度包含body高度+margin+padding+border所以html高度可能会大于body高度
		return (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
	},
	isReachBottom: function() {
		//变量scrollTop是滚动条滚动时，距离顶部的距离
		var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
		//变量windowHeight是可视区的高度
		var windowHeight = 0;
		if (document.body.clientHeight && document.documentElement.clientHeight) { 
            windowHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight); 
        } else { 
            windowHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight); 
        } 
		//变量scrollHeight是滚动条的总高度
		var scrollHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
		//滚动条到底部的条件
		return scrollTop + windowHeight + 50 >= scrollHeight
	}
}

// 登录 可传入返回地址 若没有默认返回上一页
var login={
	// 检测是否已经登录 如果未登录自动跳转至登录页
	_2page:function(target){
		var open_id = localStorage.getItem("open_id");
		if(!open_id){
			localStorage.removeItem("backPage");
			if(target){
				localStorage.setItem("backPage",target);
			}else{
				var currentUrl = window.location.href;
				localStorage.setItem('backPage', currentUrl.substring(currentUrl.indexOf("html/") + 5));
			}
			window.location.href = '../../html/' + page.mine.login;
// 			document.addEventListener('plusready', function(){
// 				var cur = plus.webview.currentWebview();
// 				cur.loadURL('../../html/' + page.mine.login);
// 			}, false);
		}else{
			return;
		}
	},
	_back:function(){
		var target = localStorage.getItem("backPage");
		if(!target){
			var tolist = getTolist();
			var topage = tolist.pop();
			if(topage){
				window.location.href = '../../html/' + topage;
			}else{
				// window.location.href = '../../html/' + page.index.index;
			}
		}else{
			window.location.href = '../../html/' + target;
		}
	}
}

try{
	document.addEventListener('plusready', function(a) {
		plus.key.addEventListener('backbutton', function() {
			var tolist = getTolist();
			var last = tolist.pop();
			if (!last) {
				backbuttonFunction();
				return;
			}
			// 如果回退到主页面 将页面栈置空
			if (last === page.index.index || last === page.pzym.index || last === page.product.index || last === page.mine.index) {
				tolist = new Array();
			}
			localStorage.setItem("tolist", JSON.stringify(tolist));
			plus.webview.currentWebview().hide();
			openWb(last);
		}, false);
	});
	var first = null;
	var backbuttonFunction = function(){
		//首次按键，提示‘再按一次退出应用’
		if (!first) {
			first = new Date().getTime();
			wx.showToast({
				title: "再按一次退出应用",
				icon: "none",
				duration: 2e3,
				mask: !0
			});//用自定义toast提示最好
			setTimeout(function() {
				first = null;
			}, 1000);
		} else {
			if (new Date().getTime() - first < 1000) {
				plus.runtime.quit();
			}
		}
	}
} catch (e){
	console.log(e);
}
